/*
 *@本js功能：在线考试详情
 *@本js作者：杨锡君
 *@编写时间：2017年11月24日
 */
$(function(){
    var Alert1 = new Alert();
    var confirm1 = new Confirm();
    var $textBox = $(".textBox");
    var stuOnlineExamDetail = {
        init:function(){
            this.bindEvent();
        },
        bindEvent:function(){
            var self = this;
            self.getExamTextEvent();


        },
        timeSubmitEvent:function(onlinequestionId,sumtime){
            var self = this;
            $(window).scroll(function(){
                if($(window).scrollTop()>60){
                    $(".timeSubmit").css({"position":"fixed","top":"10px","right":"35px"})
                }else{
                    $(".timeSubmit").css({"position":"absolute","top":"-10px","right":"0px"})
                }
            });
            // 计时
            var minutes='',seconds='',msg='';
            var maxtime =  sumtime;
            var saveNum = 1;
            var timer = setInterval(function (args) {
                saveNum++;
                if(saveNum==30){
                    self.saveEvent(onlinequestionId,0);
                    saveNum = 0;
                }
                if (maxtime > 1) {
                    minutes = Math.floor(maxtime / 60);
                    seconds = Math.floor(maxtime % 60);
                    msg =  minutes + "分" + seconds + "秒";
                    $(".timeClear").html(msg);
                    if (maxtime == 60){
                        $(".timeClear").css("color","#c80211");
                    }
                    --maxtime;
                // } else if(maxtime == 1){
                //     minutes = Math.floor(maxtime / 60);
                //     seconds = Math.floor(maxtime % 60);
                //     msg =  minutes + "分" + seconds + "秒";
                //     $(".timeClear").html(msg);
                //     Alert1.content = "时间到,停止答题";
                //     Alert1.success = function () {
                //         var json = JSON.stringify({
                //             "pageName":"stuOnlineExam"
                //         });
                //         window.location.href = "skipPage?jsonStr="+json;
                //     };
                //     Alert1.init();
                //     Alert1.show();
                //     self.submitEvent(onlinequestionId,0);
                //     --maxtime;
                }else{
                    $(".timeClear").css("color","#c80211");
                    minutes = Math.floor(maxtime / 60);
                    seconds = Math.floor(maxtime % 60);
                    msg =  minutes + "分" + seconds + "秒";
                    $(".timeClear").html(msg);
                    clearInterval(timer);
                    Alert1.content = "时间到,停止答题";
                    Alert1.success = function () {
                        var json = JSON.stringify({
                            "pageName":"stuOnlineExam"
                        });
                        window.location.href = "skipPage?jsonStr="+json;
                    };
                    Alert1.init();
                    Alert1.show();
                    self.submitEvent(onlinequestionId,0);
                }
            }, 1000);

            $(".submitText").off("click").click(function(){
                confirm1.content = "确认交卷吗";
                confirm1.success = function () {
                    self.submitEvent(onlinequestionId,1);
                };
                confirm1.init();
                confirm1.show();
            });

        },
        saveEvent:function(onlinequestionId,flags){
            var arr = [];
            for(var i = 0;i<$(".questionBox").length;i++){
                var ans = "";
                for(var j = 0;j<$(".questionBox").eq(i).find(".answerInt").length;j++){
                    if(j == $(".questionBox").eq(i).find(".answerInt").length-1){
                        ans+=$(".questionBox").eq(i).find(".answerInt").eq(j).val();
                    }else{
                        ans+=$(".questionBox").eq(i).find(".answerInt").eq(j).val()+",";
                    }

                }
                var str = { "questionId": Number($(".questionBox").eq(i).attr("data-questionid")), "ansquesContent":ans};
                arr.push(str);
            }
            var jsonStr = JSON.stringify({
                "mapList":arr,
                "onlinequestionId":onlinequestionId
            });
            $(".loadingBox").show();
            $.ajax({
                type:"post",
                url:"stuOnlineAutomaticSaveExam",
                async:true,
                data:{
                    "jsonStr":jsonStr
                },
                success:function(msg){
                    msg = JSON.parse(msg);
                    if(msg.result != "success"){
                        Alert1.content = msg.failMsg;
                        Alert1.init();
                        Alert1.show();
                        $(".loadingBox").hide();
                    }else{
                        setTimeout(function (args) {
                            $(".loadingBox").hide();
                        },3000);
                    }
                }
            });
        },
        submitEvent:function(onlinequestionId,flags){
            var arr = [];
            for(var i = 0;i<$(".questionBox").length;i++){
                var ans = "";
                for(var j = 0;j<$(".questionBox").eq(i).find(".answerInt").length;j++){
                    if(j == $(".questionBox").eq(i).find(".answerInt").length-1){
                        ans+=$(".questionBox").eq(i).find(".answerInt").eq(j).val();
                    }else{
                        ans+=$(".questionBox").eq(i).find(".answerInt").eq(j).val()+",";
                    }

                }
                var str = { "questionId": Number($(".questionBox").eq(i).attr("data-questionid")), "ansquesContent":ans};
                arr.push(str);
            }
            var choseScore = 0;
            if($(".oneChoseScore").length != 0){
                choseScore = $(".oneChoseScore").html().substring(0,$(".oneChoseScore").html().length-1);
            }
            var muchScore = 0;
            if($(".oneMuchScore").length != 0){
                muchScore = $(".oneMuchScore").html().substring(0,$(".oneMuchScore").html().length-1);
            }
            var checkScore = 0;
            if($(".oneCheckScore").length != 0){
                checkScore = $(".oneCheckScore").html().substring(0,$(".oneCheckScore").html().length-1);
            }
            var jsonStr = JSON.stringify({
                "courseId":result.paramData.courseId,
                "mapList":arr,
                "onlinequestionId":onlinequestionId,
                "optionSource":choseScore,
                "judgeSource":checkScore,
                "manyOptionSource":muchScore
            });
            $.ajax({
                type:"post",
                url:"stuOnlineSubmitExam",
                async:true,
                data:{
                    "jsonStr":jsonStr
                },
                success:function(msg){
                    msg = JSON.parse(msg);
                    if(msg.flag == true){
                        if(flags == 1){
                            Alert1.content = "试卷提交成功";
                            Alert1.success = function(){
                                var json = JSON.stringify({
                                    "pageName":"stuOnlineExam"
                                });
                                window.location.href = "skipPage?jsonStr="+json;
                            };
                            Alert1.init();
                            Alert1.show();
                        }

                    }else{
                        Alert1.content = msg.msg;
                        Alert1.init();
                        Alert1.show();
                    }
                }
            });
        },
        getExamTextEvent:function(){
            var self = this;

            var json = JSON.stringify({
                "courseId":result.paramData.courseId
            });
            $.ajax({
                type:"post",
                url:"stuOnlineExam",
                async:true,
                data:{
                    "jsonStr":json
                },
                success:function(msg){
                    msg = JSON.parse(msg);
                    if(msg.flag == true){
                        var sumtime;
                        // if(msg.mincha != ""){
                        // 	var time = msg.questonMap.examtime*60-msg.mincha;
                        var time = msg.mincha;
                        var times = time/60;
                        var min = parseInt(times);
                        var sec = time%60;
                        $(".timeClear").html(min+"分"+sec+"秒");
                        sumtime = time;
                        // }else{
                        //    $(".timeClear").html(msg.questonMap.examtime+"分钟");
                        //    sumtime = msg.questonMap.examtime*60;
                        // }
                        $(".textName").html(msg.questonMap.courseName);
                        var text = unescape(msg.questonMap.textjson);
                        text = text.replace(/\(\+\)/g,"(<input maxlength=\"200\" class=\"answerInt\" type=\"text\">)");
                        text = text.replace(/\(&emsp;&emsp;\)/g,"(<input maxlength='200' class=\"answerInt\" type=\"text\">)");
                        $textBox.html(text);
                        $('.fillSpace>input').bind('input propertychange', function() {
                            $(this).width($(this).val().length*16)
                        });
                        if(msg.questonMap.answerList!=undefined&&msg.questonMap.answerList!=null&&msg.questonMap.answerList!=''){
                            for(var i = 0;i<msg.questonMap.answerList.length;i++){
                                for(var j = 0;j< $(".questionBox").length;j++){
                                    if($(".questionBox").eq(j).attr("data-questionid") == msg.questonMap.answerList[i].questionId){
                                        $(".questionBox").eq(j).find(".answerInt").val(msg.questonMap.answerList[i].ansquesContent);
                                    }
                                }
                            }
                        }
                        self.timeSubmitEvent(msg.questonMap.onlinequestionId,sumtime);
                        $(".shortAnswer").attr("maxlength","500");
                        maxLengthLimit.init("answerInt");
                    }else{
                        Alert1.content = "获取数据失败";
                        Alert1.init();
                        Alert1.show();
                    }


                }
            });

        }
    };
    stuOnlineExamDetail.init();
});
